<template>
  <div>
    <div class="permissManaTool">
      <el-input
          size="small"
          v-model="role.name"
          placeholder="请输入角色英文名">
        <template slot="prepend">ROLE_</template>
      </el-input>
      <el-input size="small" placeholder="请输入角色中文名" v-model="role.nameZh" @keydown.enter.native="doAddRole"></el-input>
      <el-button size="small" icon="el-icon-plus" type="primary" @click="doAddRole">添加角色</el-button>
    </div>
    <div class="permissManaMain">
      <el-collapse v-model="activeName" accordion @change="change">
        <el-collapse-item
            v-for="(r, index) in roles"
            :key="index"
            :title="r.nameZh"
            :name="r.id">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>可访问资源</span>
              <el-button
                  @click="doDeleteRole(r)"
                  icon="el-icon-delete"
                  style="float: right; padding: 3px 0;color: #ff0000"
                  type="text"></el-button>
            </div>
            <div>
              <el-tree
                  :default-checked-keys="selectMenus"
                  node-key="id"
                  show-checkbox
                  ref="tree"
                  :key="index"
                  :data="allMenus"
                  :props="defaultProps">
              </el-tree>
              <div style="display: flex; justify-content: flex-end">
                <el-button @click="cancelModification" size="small">取消修改</el-button>
                <el-button
                    @click="doUpdate(r.id,index)"
                    size="small"
                    type="primary">确认修改</el-button>
              </div>
            </div>
          </el-card>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script>
export default {
  name: "PermissMana",
  data(){
    return {
      role: {
        id: 0,
        name: '',
        nameZh: '',
      },
      roles:[],
      allMenus: [],
      defaultProps: {
        children: 'children',
        label: 'name',
      },
      selectMenus: [],
      activeName: -1,
    };
  },
  mounted() {
    this.initRoles();
  },
  methods:{
    // 删除角色
    doDeleteRole(role){
      this.$confirm(`此操作将永久删除[${role.nameZh}]角色，是否继续？`,'提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(()=>{
        this.deleteRequest(`/system/basic/permiss/role/${role.id}`).then((resp)=>{
          if (resp){
            this.initRoles();
          }
        });
      })
    },

    // 添加角色内容
    doAddRole(){
      if (this.role.name && this.role.nameZh){
        this.postRequest('/system/basic/permiss/role', this.role).then((resp)=>{
          if (resp){
            // 初始化角色信息
            this.initRoles();
            // 清空输入框中的内容
            this.role.nameZh = '';
            this.role.name = '';
          }
        });
      }else {
        this.$message.error('所有字段不能为空');
      }
    },
    // 取消修改
    cancelModification(){
      this.selectMenus = [];
      this.activeName = -1;
    },
    // 更新
    doUpdate(rid,index){
      let tree = this.$refs.tree[index];
      let selectKeys = tree.getCheckedKeys();
      let url = '/system/basic/permiss/?rid='+rid;
      selectKeys.forEach(key=>{
        url += '&mids='+key;
      });
      // 发送请求
      this.putRequest(url).then((resp)=>{
        if (resp){
          // 关闭折叠面板
          this.activeName = -1;
          // 清空改角色默认选中的权限
          this.roles = [];
          this.initRoles();
        }
      })
    },
    // 接收点击具体的手风琴
    change(rid){
      if(rid){
        // 获取该角色拥有的权限
        this.initSelectMenus(rid);
        // 获得全部的菜单
        this.initAllMenus();
      }
    },
    // 根据角色id获取菜单的id
    initSelectMenus(rid){
      this.getRequest(`/system/basic/permiss/mid/${rid}`).then(resp=>{
        if (resp){
          this.selectMenus = resp;
        }
      })
    },
    // 获取
    initAllMenus(){
      this.getRequest('/system/basic/permiss/menus').then(resp=>{
        if (resp){
          this.allMenus = resp;
        }
      });
    },
    // 获取所有角色
    initRoles(){
      // 请求后端接口，得到数据
      this.getRequest('/system/basic/permiss/role').then(resp=>{
        if (resp){
          this.roles = resp;
        }
      })
    },
  }
}
</script>

<style >
  .permissManaTool{
    display: flex;
    justify-content: flex-start;
  }
  .permissManaTool .el-input{
    width: 300px;
    margin-right: 6px;
  }
  .permissManaMain{
    margin-top: 10px;
    width: 700px;
  }
</style>